package com.gzwanchuang.xhgj.ui.page.mine;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.HeHistoryViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentHeHistoryBinding;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Mergedintoaccount;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.Calendar;
import java.util.List;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class HeHistoryFragment extends BaseFragment {


    private FragmentHeHistoryBinding mBinding;
    private HeHistoryViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(HeHistoryViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_he_history, container, false);
        mBinding = FragmentHeHistoryBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        Mergedintoaccount.old_query.Builder builders = Mergedintoaccount.old_query.newBuilder();
        builders.setStart(mViewModel.timeOne.get()).setEnd(mViewModel.timeTwo.get());
        NetworkManager.INSTANCE.post(Apis.old_query, builders.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {

                Mergedintoaccount.old_query old_query = Mergedintoaccount.old_query.parseFrom(response);
                Log.e("old_query", old_query.toString());
                List<Mergedintoaccount.record> orders = old_query.getRecordListList();
                mViewModel.data.addAll(orders);
                mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showShortToast(errorMessage);
            }
        });


    }

    @Override
    public void loadInitData() {
        super.loadInitData();
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            Mergedintoaccount.record bean = mViewModel.data.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("cash_no", bean.getOrderNo());
            nav().navigate(R.id.action_heHistoryFragment_to_hheZhangDetailFragment,bundle);
        }

        public void loadMoreListener() {

        }

        public void check() {
            showProgress();
            Mergedintoaccount.old_query.Builder builders = Mergedintoaccount.old_query.newBuilder();
            builders.setStart(mViewModel.timeOne.get()).setEnd(mViewModel.timeTwo.get());
            NetworkManager.INSTANCE.post(Apis.old_query, builders.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    hideProgress();
                    mViewModel.data.clear();
                    Mergedintoaccount.old_query old_query = Mergedintoaccount.old_query.parseFrom(response);
                    Log.e("old_query", old_query.toString());
                    List<Mergedintoaccount.record> orders = old_query.getRecordListList();
                    mViewModel.data.addAll(orders);
                    mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                    showShortToast(errorMessage);
                }
            });

        }


        public void timeOne() {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(mActivity, mViewModel.lisOne,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }

        public void timeTwo() {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(mActivity, mViewModel.lisTwo,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
    }
}
